package com.common.base.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.Typeface
import android.util.AttributeSet
import android.widget.TextView
import com.common.base.R

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：4/5/2023
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class RoundNumTextView : TextView {

    private val textRect = RectF()
    private val textPaint = Paint()
    private val textBgPaint = Paint()
    private val textPadding = 10f // 文字背景上下边距
    private val bgWidth = 80f // 固定文字背景宽度
    private var bgGap = 5f // 文字背景左右间距

    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(
        context!!, attrs
    ) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context!!, attrs, defStyleAttr
    ) {
        init()
    }

    private fun init() {
        textPaint.color = context.getColor(R.color.c_970e00) // 文字的背景颜色
        textPaint.textSize = textSize
        textPaint.typeface = Typeface.DEFAULT_BOLD

        textBgPaint.color = Color.WHITE // 文字的背景颜色
        textBgPaint.textSize = textSize
    }

    override fun onDraw(canvas: Canvas) {

        val text = text.toString()

        val ascent = -textBgPaint.ascent()
        val descent = textBgPaint.descent()
        val textSize = ascent + descent
        val baseLine: Float = height - (height - textSize) / 2f

        var x = bgGap+bgWidth / 2f
        for (element in text) {
            val chTemp = element
            if (isPunc(chTemp)) { // 判断是否为标点符号
                val textWidth = textBgPaint.measureText(chTemp.toString())
                val centerX = x - bgWidth / 2f // 这里需要加上标点符号的宽度一半
                val centerY = baseLine - textSize / 2f
                val textY = centerY + (textSize - descent) / 2f
                canvas.drawText(chTemp.toString(), centerX, textY, textBgPaint)
                x += textWidth + bgGap // 加上已绘制的标点符号的宽度和文字背景与标点符号之间的间距
            } else {
                val textWidth = bgWidth - bgGap * 2
                val textHeight = textSize + textPadding * 2
                val centerY = baseLine - textSize / 2f // 将文字背景的中心点位置与文字的中心点位置对齐
                textRect[x - textWidth / 2f, centerY - textHeight / 2f, x + textWidth / 2f] =
                    centerY + textHeight / 2f
                canvas.drawRoundRect(textRect, 18f, 18f, textBgPaint) // 绘制文字背景

                // 计算矩形的中心坐标
                val centerXTemp = textRect.centerX()
                val centerYTemp = textRect.centerY()

                // 计算文字的起始坐标
                val textWidthTemp = textBgPaint.measureText(chTemp.toString()) // 获取文字宽度
                val textXTemp = centerXTemp - textWidthTemp / 2f
                val textYTemp = centerYTemp + (textSize - descent) / 3f
                canvas.drawText(chTemp.toString(), textXTemp, textYTemp, textPaint) // 绘制文字

                x += bgWidth + bgGap // 加上文字背景的固定宽度和标点符号与文字背景之间的间距
            }
        }
    }

    /**
     * 判断是否为标点符号
     * @param ch 字符
     * @return 是否为标点符号
     */
    private fun isPunc(ch: Char): Boolean {
        return ch == '.' || ch == ',' || ch == '!' || ch == '?' || ch == ':' || ch == ';' || ch == '\'' || ch == '\"' || ch == '(' || ch == ')' || ch == '[' || ch == ']' || ch == '{' || ch == '}'
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) {
           val textWidth = measureTextWidth(text.toString())
            setMeasuredDimension((textWidth + paddingLeft + paddingRight).toInt(), measuredHeight + (textPadding * 2).toInt())
        }
    }

    /**
     * 计算文本宽度
     */
    private fun measureTextWidth(text: String): Float {
        var width = 0f
        for (element in text) {
            width += if (isPunc(element)) {
                 textPaint.measureText(element.toString()) + bgGap
            } else {
                bgWidth + bgGap
            }
        }
        return width
    }
}